<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>显示二维码</title>
    <style>
        .center{text-align: center;}
        #url{display: none;}
    </style>
</head>
<body>
<h2 class="center">微信支付</h2>
<div class="center">
    <img id="qrcode" src="" alt="" width="50%" />
    <input type="hidden" id="out_trade_no" value="{$product_id}" >
</div>
<p id="url">{$qrCode_url}</p>
<script type="text/javascript" src="__STATIC__/js/jquery-3.3.1.min.js"></script>
<script>
    $(function () {
        var qrcodeurl = $('#url').text();
        $.ajax({
            url:"{:url('wxpay/create')}",
            data:{'qrcodeurl':qrcodeurl},
            type:'post',
            dataType:'json',
            success:function(result){
                if (result.code==200) {
                    //console.log(r);
                    var path = 'data:image/png;base64,'+result.data;
                    //给img的sec赋值。
                    $("#qrcode").attr('src',path);
                } else {
                    alert(r.err);
                }
            },
            error:function () {
                console.log('请求出现问题！请检查');
            }
        });

    });
    // 产看订单状态
    var time = setInterval("check()",3000);    //3秒查询一次是否支付成功
    function check() {
        var url = "{:url('/index/Wxpay/orderstate')}";
        var out_trade_no = $("#out_trade_no").val();
        var param = {'out_trade_no':out_trade_no};
        $.post(url,param,function(data){
            var obj = eval(data);
            if (obj.trade_state == 'SUCCESS') {
                time = window.clearInterval(time);
                // 支付成功把二维码替换成支付成功图标
                $("#qrcode").attr('src','/public/wxpay/images/success.png');
                console.log(obj);
            }else{
                console.log(obj);
            }
        });
    }
</script>
</body>
</html>